package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.TechInstructionMapper;
import com.zhijian.medical.entity.po.TechInstruction;
import com.zhijian.medical.entity.vo.request.TechInstructionSearchVo;
import com.zhijian.medical.entity.vo.request.TechInstructionSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.TechInstructionVo;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.TechInstructionService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 技术医嘱业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2024-11-05 17:17:40
 */
@Slf4j
@Service
public class TechInstructionServiceImpl implements TechInstructionService {

	@Autowired
    private TechInstructionMapper mapper;

    @Override
    public TechInstruction selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TechInstructionVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(TechInstruction techInstruction) {
        Date now = new Date();
        techInstruction.setCreateTime(now);
        techInstruction.setUpdateTime(now);
        return mapper.insert(techInstruction) > 0 ? techInstruction.getId() : 0L;
    }

    @Override
    public Long insertSelective(TechInstruction techInstruction) {
        Date now = new Date();
        techInstruction.setCreateTime(now);
        techInstruction.setUpdateTime(now);
        return mapper.insertSelective(techInstruction) > 0 ? techInstruction.getId() : 0L;
    }

    @Override
    public boolean update(TechInstruction techInstruction) {
        Date now = new Date();
        techInstruction.setUpdateTime(now);
        return mapper.updateByPrimaryKey(techInstruction) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(TechInstruction record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(TechInstructionSaveOrUpdateVo saveOrUpdateVo) {
        TechInstruction techInstruction = new TechInstruction();
        BeanUtils.copyProperties(saveOrUpdateVo, techInstruction);
        techInstruction.setWritePerson(CurrentUserHolder.getUser().getName());
        return this.insertSelective(techInstruction) != 0;
    }

    @Override
    public boolean modify(TechInstructionSaveOrUpdateVo saveOrUpdateVo) {
        TechInstruction old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        TechInstruction techInstruction = new TechInstruction();
        BeanUtils.copyProperties(saveOrUpdateVo, techInstruction);
        return this.update(techInstruction);
    }

    @Override
    public TechInstructionVo selectOne(TechInstructionSearchVo techInstructionSearchVo) {
        return mapper.selectOne(techInstructionSearchVo);
     }

    @Override
    public Integer count(TechInstructionSearchVo techInstructionSearchVo) {
        return mapper.count(techInstructionSearchVo);
    }

    @Override
    public List<TechInstructionVo> selectList(TechInstructionSearchVo techInstructionSearchVo) {
        return mapper.selectList(techInstructionSearchVo);
    }

    @Override
    public Page<TechInstructionVo> selectPage(TechInstructionSearchVo techInstructionSearchVo) {
        AssertUtil.check(Objects.isNull(techInstructionSearchVo.getPageNum()) || Objects.isNull(techInstructionSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(techInstructionSearchVo);
        //分页
        Page<TechInstructionVo> page = new Page<>(true, count, techInstructionSearchVo.getPageNum(), techInstructionSearchVo.getPageSize());
        techInstructionSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TechInstructionVo> responseVos = this.selectList(techInstructionSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<TechInstruction> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(TechInstructionSearchVo techInstructionSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(techInstructionSearchVo);
    }
    
}